<template>
  <div>
    <!--tab选择栏--医院 -->
    <div ref="search" class="app-search">
      <el-row type="flex" justify="space-between">
        <el-col :span="24" align="right" class="addBtnStyle">
          <el-button @click="handleAdd(true)">+增加角色</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="box-show marginTop" :style="'height:'+ (innerHeight + 80) + 'px'">
      <el-table v-loading="loading" :data="list" border :row-class-name="tableColor" :height="innerHeight">
        <el-table-column prop="id" label="编号" :show-overflow-tooltip="true" />
        <el-table-column prop="roleName" label="角色名称" :show-overflow-tooltip="true" />
        <el-table-column prop="hospitalName" label="所属医院" :show-overflow-tooltip="true" />
        <el-table-column prop="status" label="状态" :show-overflow-tooltip="true" />
        <el-table-column label="操作" :show-overflow-tooltip="true" min-width="120">
          <template slot-scope="{ row }">
            <!-- <el-button type="text" size="small">查看</el-button>-->
            <el-button type="text" size="small" @click="handleAdd(true,row)">编辑</el-button>
            <el-button slot="reference" type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-num="queryParams.page" :page-size="queryParams.pageSize" :on-change="handlePaginationChange" :total="total" />
    </div>
    <Dialog :title="title" button-text="保存" class="single-column" :loading="loading" :visible="visible" @close="handleAdd(false,{})" @confirm="confirm">
      <addRole v-if="visible" ref="addRole" :row="currentRow" :is-edit.sync="isEdit" @success="handleSuccess" />
    </Dialog>
  </div>
</template>
<script>
import Dialog from "@/components/Dialog";
import { tableColor, labelHeigh } from '@/utils/common.js';
import addRole from "./addRole";
export default {
  components: {
    Dialog,
    addRole,
  },
  data() {
    return {
      title: '增加角色',
      visible: false,
      reveal: false,
      buttonLoading: false,
      currentRow: {},
      value: '',
      input3: '',
      list: [],
      tableColor,
      innerHeight: null,
      queryParams: {
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      },
      total: 0,
      loading: false,
      isEdit: false,
    };
  },
  mounted() {
    this.getList();
    this.innerHeight = labelHeigh(this.$refs.search) - 30;
  },
  methods: {
    handleAdd(bool, row) {
      if (bool && row) this.title = "编辑角色";
      if (bool && !row) this.title = "增加角色";
      this.visible = bool;
      this.currentRow = {};
      if (row) this.currentRow = JSON.parse(JSON.stringify((row)));
    },
    async handleDelete(row) {
      const confirmResult = await this.$confirm('请确认是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除');
      }
      this.buttonLoading = true;
      this.$api['data/role/deleteHisrole']({ id: row.id }).then(res => {
        if (res.code !== 200) {
          this.$message.error(res.msg);
          this.buttonLoading = false;
          return false;
        }
        this.msgSuccess('删除成功');
        this.getList();
      }).finally(() => (this.buttonLoading = false));
    },
    handlePaginationChange(pagination) {
      this.queryParams = { page: pagination.pageNum, pageSize: pagination.pageSize };
      this.getList();
    },
    getList() {
      this.loading = true;
      this.$api['data/role/hisrole']({
        ...this.queryParams,
      }).then(res => {
        if (res.code !== 200) return;
        this.list = res.data.result;
        this.total = res.data.total;
      }).finally(() => (this.loading = false));
    },
    handleSuccess() {
      this.visible = false;
      this.loading = false;
      this.getList();
    },
    clean() {
      this.search = '';
    },
    confirm() {
      this.loading = true;
      this.$refs.addRole.submitForm('ruleForm');
    },
  },
};
</script>
